package com.spic.business.mountguard.service.impl;

import com.spic.business.common.core.utils.DateUtil;
import com.spic.business.mountguard.domain.LeadersigninConfig;
import com.spic.business.mountguard.domain.MountGuardLeadersignin;
import com.spic.business.mountguard.mapper.LeadersigninConfigMapper;
import com.spic.business.mountguard.mapper.MountGuardLeadersigninMapper;
import com.spic.business.mountguard.service.IMountGuardLeadersigninService;
import com.spic.business.mountguard.service.IMountGuardQrService;
import com.spic.common.core.domain.R;
import com.spic.system.api.RemoteNoticeInfoService;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysNoticeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 领导上岗到位签到Service业务层处理
 *
 * @author yangjie
 * @date 2021-11-01
 */
@Service
public class MountGuardLeadersigninServiceImpl implements IMountGuardLeadersigninService
{
    @Autowired
    private MountGuardLeadersigninMapper mountGuardLeadersigninMapper;
    @Autowired
    private IMountGuardQrService mountGuardQrService;
    @Autowired
    private LeadersigninConfigMapper leadersigninConfigMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteNoticeInfoService remoteNoticeInfoService;

    /**
     * 查询领导上岗到位签到
     *
     * @param id 领导上岗到位签到主键
     * @return 领导上岗到位签到
     */
    @Override
    public MountGuardLeadersignin selectMountGuardLeadersigninById(Integer id)
    {
        return mountGuardLeadersigninMapper.selectMountGuardLeadersigninById(id);
    }

    /**
     * 查询领导上岗到位签到列表
     *
     * @param mountGuardLeadersignin 领导上岗到位签到
     * @return 领导上岗到位签到
     */
    @Override
    public List<MountGuardLeadersignin> selectMountGuardLeadersigninList(MountGuardLeadersignin mountGuardLeadersignin)
    {
        return mountGuardLeadersigninMapper.selectMountGuardLeadersigninList(mountGuardLeadersignin);
    }

    /**
     * 新增领导上岗到位签到
     *
     * @param mountGuardLeadersignin 领导上岗到位签到
     * @return 结果
     */
    @Override
    public int insertMountGuardLeadersignin(MountGuardLeadersignin mountGuardLeadersignin)
    {

        int count = -1;
        count = mountGuardLeadersigninMapper.insertMountGuardLeadersignin(mountGuardLeadersignin);

        return count;
    }

    /**
     * 修改领导上岗到位签到
     *
     * @param mountGuardLeadersignin 领导上岗到位签到
     * @return 结果
     */
    @Override
    public int updateMountGuardLeadersignin(MountGuardLeadersignin mountGuardLeadersignin)
    {
        return mountGuardLeadersigninMapper.updateMountGuardLeadersignin(mountGuardLeadersignin);
    }

    /**
     * 批量删除领导上岗到位签到
     *
     * @param ids 需要删除的领导上岗到位签到主键
     * @return 结果
     */
    @Override
    public int deleteMountGuardLeadersigninByIds(Integer[] ids)
    {
        return mountGuardLeadersigninMapper.deleteMountGuardLeadersigninByIds(ids);
    }

    /**
     * 删除领导上岗到位签到信息
     *
     * @param id 领导上岗到位签到主键
     * @return 结果
     */
    @Override
    public int deleteMountGuardLeadersigninById(Integer id)
    {
        return mountGuardLeadersigninMapper.deleteMountGuardLeadersigninById(id);
    }

    //统计当月签到次数集合
    @Override
    public List<HashMap<String, Object>> selectSignCount()
    {
        List<HashMap<String, Object>> hashMaps = mountGuardLeadersigninMapper.selectSignCount();
        List<LeadersigninConfig> leadersigninConfigs = leadersigninConfigMapper.selectLeadersigninConfigList(null);
        R<List<Map<String, Object>>> postUser = remoteUserService.getPostUser();
        if(postUser!=null){
            int planCount = 0;
            List<Map<String, Object>> data = postUser.getData();

            String format = DateUtil.format(new Date(), "yyyy-MM");
            List<HashMap<String, Object>> newMapList = new ArrayList<>();
            for (HashMap<String, Object> hashMap : hashMaps)
            {
                int count = Integer.parseInt(hashMap.get("COUNT")+"");
                String userId = hashMap.get("user_id")+"";
                String times = (String) hashMap.get("times");
                if(format.equals(times)){
                    for (Map<String, Object> datum : data)
                    {
                        String post_id = datum.get("post_id")+"";
                        String user_id = datum.get("user_id")+"";
                        if(userId.equals(user_id)){
                            for (LeadersigninConfig leadersigninConfig : leadersigninConfigs)
                            {
                                String postId = leadersigninConfig.getPostId()+"";
                                if(postId.equals(post_id)){
                                     planCount = leadersigninConfig.getPlanCount();
                                     if(planCount>count){
                                         //发提醒
                                         SysNoticeInfo sysNoticeInfo = new SysNoticeInfo();
                                         sysNoticeInfo.setContent("您本月上岗到位签到次数不足");
                                         sysNoticeInfo.setTitle("上岗到位不足");
                                         sysNoticeInfo.setUserId(userId);
                                         remoteNoticeInfoService.add(sysNoticeInfo);
                                     }
                                }
                            }
                        }
                    }
                    newMapList.add(hashMap);
                }
            }
            return newMapList;
        }
        return null;
    }

    /**
     * 二维码状态
     * @return
     */
    @Override
    public Boolean selectQrCodeState(String key)
    {
        return  mountGuardQrService.selectQrCodeState(key);
    }



}
